package com.minigfxdemo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.objetdirect.tatami.client.gfx.Color;
import com.objetdirect.tatami.client.gfx.GraphicCanvas;
import com.objetdirect.tatami.client.gfx.GraphicObject;
import com.objetdirect.tatami.client.gfx.GraphicObjectListener;
import com.objetdirect.tatami.client.gfx.Rect;
import com.objetdirect.tatami.client.gfx.Text;

public class MiniGFXDemo extends Composite implements GraphicObjectListener, ClickListener, ChangeListener, EntryPoint{
	private GraphicCanvas canvas;
	private Color currentStrokeColor = Color.BLACK;
	private Color currentFillColor = Color.WHITE;
	private int currentStrokeSize = 1;
	private DockPanel panel = new DockPanel();
	private void showGraphicObject(GraphicObject object, int x, int y) {
			object.setStroke(currentStrokeColor, currentStrokeSize);			
			object.setFillColor(currentFillColor);			
			this.canvas.add(object, x, y);
	}
	
	public static int aleatorio(int max,int min){
		return (int)(Math.random()*(max-min))+min;		
    }

	public void onModuleLoad() {
		    int x = 0;
		    int y = 0;
		    int x1 = 0;
		    int y1 = 0;
		    double width = 0;
		    double height = 0;
	        try {
				canvas = new GraphicCanvas();				
				canvas.setPixelSize(1200, 1200);
				canvas.addGraphicObjectListener(this);
				for (short k=1;k<3;k++){
					for (short i=0;i<10;i++){
					   x1 = 103*i;
					   y1 = 105*k;
					   currentStrokeColor = Color.BLACK;
					   currentFillColor = Color.WHITE;
					   RoomRect room = new RoomRect(100,100);
					   room.setFloor(k);
					   room.setRoom(i);
					   room.setBeds(aleatorio(1,7));					   
					   this.showGraphicObject(room,x1, y1);
					   this.showGraphicObject(new Text(""+i), x1, 115*k);
					   height = room.getHeight()/2;
					   if (room.getBeds()>1) {
						   width = room.getWidth() / (room.getBeds()-1);
					   }else{
						   width  = room.getWidth()/2;
					   }					   
					   y = y1;
					   x = x1;
					   for (short j=0;j<room.getBeds();j++){
						   currentStrokeColor = Color.RED;
						   currentFillColor = Color.WHITE;
						   BedRect bed = new BedRect(width,height);
						   bed.setRoom(room);
						   bed.setNumber(j);
						   if (j==(room.getBeds()/2)){
							   y = y + (int)height;
							   x = x1;
						   }
						   this.showGraphicObject(bed,x,y);
						   this.showGraphicObject(new Text(bed.getNumber()+""), (int)bed.getX()+10, (int)bed.getY()+10);
						   x = x + (int)width;					   
					   }
					   
					}
				}
				panel.add(canvas, DockPanel.CENTER);
				RootPanel.get().add(panel);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Window.alert(e.getMessage());
			}
	}

	@Override
	public void onClick(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onChange(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDblClicked(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		try {
			if (graphicObject instanceof RoomRect){
				RoomRect room = (RoomRect)graphicObject;
				Window.alert("floor:"+room.getFloor()+",room:"+room.getRoom()+",x:"+room.getX()+",y:"+room.getY());
			}else{
				BedRect bed = (BedRect)graphicObject;
				Window.alert("floor:"+bed.getRoom().getFloor()+",room:"+bed.getRoom().getRoom()+",bed:"+bed.getNumber());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Window.alert(e.getMessage());
		}
	}
}
